'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'                                                                         '
'   Copyright (C) 2008 by Daniel Danopia								  '
'   me.L0GG3R@danopia.net												  '
'                                                                         '
'   This program is free software; you can redistribute it and/or modify  '
'   it under the terms of the GNU General Public License as published by  '
'   the Free Software Foundation; either version 2 of the License, or     '
'   (at your option) any later version.                                   '
'                                                                         '
'   This program is distributed in the hope that it will be useful,       '
'   but WITHOUT ANY WARRANTY; without even the implied warranty of        '
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         '
'   GNU General Public License for more details.                          '
'                                                                         '
'   You should have received a copy of the GNU General Public License     '
'   along with this program; if not, write to the                         '
'   Free Software Foundation, Inc.,                                       '
'   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             '
'                                                                         '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Imports MySql.Data.MySqlClient
Public Class Database
    Public Shared Server As String
    Public Shared User As String
    Public Shared Pass As String
    Public Shared Database As String
    Public Shared Servers As New Dictionary(Of String, Integer)
    Private Shared Connection As New MySqlConnection
    Public Shared Function Connect() As Boolean
        Connection.Close()
        Threading.Thread.Sleep(1000)
        Connection.ConnectionString = "Database=" & Database & ";Data Source=" & Server & ";User Id=" & User & ";Password=" & Pass & ""
        Try
            Connection.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return (Connection.State = ConnectionState.Connecting) Or (Connection.State = ConnectionState.Open)
    End Function
    Public Shared Sub ReloadConfig()
        Database = Form1.Config("db_database")
        Pass = Form1.Config("db_pass")
        Server = Form1.Config("db_server")
        User = Form1.Config("db_user")
    End Sub
    Public Shared Function IsConnected() As Boolean
        Return Connection.State = ConnectionState.Open
    End Function
    Public Shared Sub Close()
        Connection.Close()
    End Sub
    Private Shared Function CreateCommand(ByVal SQL As String) As MySqlCommand
        CreateCommand = Connection.CreateCommand
        CreateCommand.CommandText = SQL
    End Function
    Public Shared Sub ExecuteNonQuery(ByVal SQL As String)
        Try
            CreateCommand(SQL).ExecuteNonQuery()
        Catch ex As Exception
            Connect()
            CreateCommand(SQL).ExecuteNonQuery()
        End Try
    End Sub
    Public Shared Function ExecuteScalar(ByVal SQL As String) As Object
        Try
            Return CreateCommand(SQL).ExecuteScalar()
        Catch ex As Exception
            Connect()
            Return CreateCommand(SQL).ExecuteScalar()
        End Try
    End Function
    Public Shared Function ExecuteReader(ByVal SQL As String) As MySqlDataReader
        Try
            Return CreateCommand(SQL).ExecuteReader()
        Catch ex As Exception
            Connect()
            Return CreateCommand(SQL).ExecuteReader()
        End Try
    End Function
    Public Shared Function ExecuteRow(ByVal SQL As String) As Object()
        Dim Reader As MySqlDataReader
        Try
            Reader = CreateCommand(SQL).ExecuteReader()
        Catch ex As Exception
            Connect()
            Reader = CreateCommand(SQL).ExecuteReader()
        End Try
        If Reader.HasRows Then
            Reader.Read()
            ExecuteRow = New Object(Reader.FieldCount) {}
            Reader.GetValues(ExecuteRow)
            Reader.Close()
        Else
            Return Nothing
        End If
    End Function
    Public Shared Sub LoadTables()
        If Not IsConnected() Then Connect()
        Servers.Clear()
        Dim Reader As MySqlDataReader = ExecuteReader("SELECT server_id, server_name FROM lgr_servers")
        While Reader.Read
            If Servers.ContainsKey(Reader.GetValue(0)) Then
                Servers(Reader.GetValue(1)) = Reader(0)
            Else
                Servers.Add(Reader.GetValue(1), Reader(0))
            End If
        End While
        Reader.Close()
    End Sub
    Public Shared Function GetRow(ByVal Reader As MySqlDataReader) As Dictionary(Of String, Object)
        GetRow = New Dictionary(Of String, Object)
        For i As Integer = 0 To Reader.FieldCount - 1
            GetRow.Add(Reader.GetName(i), Reader.GetValue(i))
        Next
    End Function
End Class